package com.eascs.spider.ant.worker.spider;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.eascs.spider.ant.common.proj.Platform;
import com.eascs.spider.ant.common.proj.Task;
import com.eascs.spider.ant.common.util.CommonUtil;
import com.eascs.spider.ant.common.util.Downloader;

/**
 * 中关村手机报价
 * @author hzp
 *
 */
public class ZolPhoneSpider extends AnotherSpider{

	public ZolPhoneSpider(Task task) {
		super(task);
	}

	@Override
	public void run() {
		String regex = "http://detail.zol.com.cn/cell_phone_index/subcate57_(\\d+)_list_1_0_1_1_0_1.html";
		Matcher matcher = Pattern.compile(regex).matcher(task.getUrl());
		if(matcher.find()){
			int code = Integer.valueOf(matcher.group(1));
			if(code == 0){
				crawlerHomePage();
			}else{
				crawlerBrand();
			}
		}else{
			logger.warn("其他未经处理的url：{}" ,task.getUrl());
		}
		
	}
	
	//从手机大全主页开始爬各品牌url
	private void crawlerHomePage(){
		//获取列表展示形式各品牌url
		String homePageUrl = task.getUrl();
		Map<String, String> header = new HashMap<String, String>();
		header.put("Referer", "http://detail.zol.com.cn/");
		header.put("Host", "detail.zol.com.cn");
		Downloader downloader = Downloader.build()
										  .setUrl(homePageUrl)
										  .setHttpMethod(Task.HTTP_GET)
										  .setHeader(header)
										  .download();
		String html = downloader.getHtml();
		Document document = Jsoup.parse(html);
		Elements aes = document.select("div#J_BrandAll a");
		for(Element a : aes){
			String brand = a.text();
			String url = "http://detail.zol.com.cn" + a.attr("href");
			Task task = new Task();
			task.setUrl(url)
				.setMethod(Task.HTTP_GET)
				.setHomepage(Task.HOMEPAGE_NO)
				.setHeaderMap(header)
				.setRemark(brand);
			manager.addSendQueue(task);
		}
	}
	
	//指定某个品牌的爬取
	private void crawlerBrand(){
		String url = task.getUrl();
		Map<String, String> header = new HashMap<>();
		header.put("Referer", "http://detail.zol.com.cn/cell_phone_index/subcate57_0_list_1_0_1_1_0_1.html");
		header.put("Host", "detail.zol.com.cn");
		Downloader downloader = Downloader.build()
									      .setUrl(url)
									      .setHttpMethod(Task.HTTP_GET)
									      .setHeader(header)
									      .download();
		String html = downloader.getHtml();
		etl(html);
		turnpage(html);
	}
	
	//翻页
	public void turnpage(String html){
		boolean hasNextPage = true;
		Document document = Jsoup.parse(html);
		Element a = document.selectFirst("a.next");
		String nextPageUrl = null;
		if(a != null){
			nextPageUrl = "http://detail.zol.com.cn" + a.attr("href");
		}else{
			hasNextPage = false;
		}
		while(hasNextPage){
			Map<String, String> header = task.getHeaderMap();
			header.put("Referer", downloader.getUrl());//用上次请求的url做此次翻页的Referer
			downloader.setHeader(header)
					  .setUrl(nextPageUrl)
					  .setHttpMethod(Task.HTTP_GET)
					  .download();
			html = downloader.getHtml();
			etl(html);
			Document docTemp = Jsoup.parse(html);
			Element aTemp = docTemp.selectFirst("a.next");
			if(aTemp != null){
				nextPageUrl = "http://detail.zol.com.cn" + aTemp.attr("href");
			}else{
				hasNextPage = false;
			}
			CommonUtil.sleep(3000);
		}
	}
	
	//从html提取手机具体信息
	public void etl(String html){
		Document document = Jsoup.parse(html);
		Elements items = document.select("div[class=list-item clearfix]");
		for(Element item : items){
			Map<String, Object> data = new HashMap<>();
			data.put("crawler_time", new Date(System.currentTimeMillis()));
			data.put("brand", task.getRemark());
			String name = item.selectFirst("h3").text();
			String url = "http://detail.zol.com.cn" + item.selectFirst("h3 a").attr("href");
			data.put("name", name);
			data.put("url", url);
			logger.debug("name:{},url:{}" ,name,url);
			handlerForSingleProduct(data);//爬各种报价
			Elements lies = items.select("ul[class=param clearfix] li");
			for(Element li : lies){
				String[] temp = li.text().replace("更多参数", "").split("：");
				logger.debug("参数,{}:{}" ,temp[0],temp[1]);
				data.put(temp[0], temp[1]);
			}
			if(item.selectFirst("b.price-type") != null){
				String price_type = item.selectFirst("b.price-type").text();
				if(price_type.contains("概念产品") || price_type.contains("即将上市")){
					data.put("type", price_type);
					mongoSession.setData(data, Platform.ZOL_PHONE);
					continue;
				}
				data.put("price", price_type);
			}
			
			String date = null;
			if(item.selectFirst("span.date") != null){
				date = item.selectFirst("span.date").text();
				data.put("date", date);
			}
			
			String mernum = null;
			if(item.selectFirst("p.mernum") != null){
				mernum = item.selectFirst("p.mernum").text();
				data.put("mernum", mernum);
			}
			//查询低价链接
			String low_price_url = null;
			if(item.selectFirst("a.base") != null){
				low_price_url = "http://detail.zol.com.cn" + item.selectFirst("a.base").attr("href");
				data.put("low_price_url", low_price_url);
			}
			
			logger.info("单条数据:{}" ,data.toString());
			mongoSession.setData(data, Platform.ZOL_PHONE);
		}
	}

	//处理某个产品url，从中提取本地报价，商家报价，电商报价
	public void handlerForSingleProduct(Map<String, Object> data){
		Map<String, String> header = new HashMap<>();
		header.put("Referer", downloader.getUrl());
		header.put("Host", "detail.zol.com.cn");
		Downloader downloader = Downloader.build()
										  .setUrl((String)data.get("url"))
										  .setHttpMethod(Task.HTTP_GET)
										  .setHeader(header)
										  .download();
		String html = downloader.getHtml();
		Document document = Jsoup.parse(html);
		if(document.selectFirst("div.price__reference div[class=price price-normal]") !=null){
			String price_reference = document.selectFirst("div.price__reference div[class=price price-normal]").text();
			data.put("参考报价", price_reference);
		}
		if(document.selectFirst("dd#_j_local_price") != null){
			String price_merchant = document.selectFirst("dd#_j_local_price").text();
			data.put("商家报价", price_merchant);
		}
		if(document.select("dl.price__b2c li a") != null){
			Elements aes = document.select("dl.price__b2c li a");
			for(int i=0;i<aes.size();i+=2){
				data.put("在售电商报价之" + aes.get(i).text(), aes.get(i+1).text());
			}
		}
		CommonUtil.sleep(2000);
	}
}
